fix(npm): merge NODE_OPTIONS and drop quotes in shadow --node-options - #1433
Open
John-David Dalton (jdalton) wants to merge 1 commit into
Open
fix(npm): merge NODE_OPTIONS and drop quotes in shadow --node-options#1433John-David Dalton (jdalton) wants to merge 1 commit into
John-David Dalton (jdalton) wants to merge 1 commit into
Conversation
The shadow npm wrapper built npm's `--node-options` value as:
--node-options='<user value><perm flags>'
Two bugs on that one line:
1. Node >= 24 crash (#1160). spawn() runs npm without a shell, so the
literal single quotes became part of the NODE_OPTIONS value npm set
for lifecycle scripts. Consumers that re-tokenize NODE_OPTIONS on
whitespace (e.g. Next.js) only understand `"`, not `'`, so they
dropped `'--permission` (leading quote) while keeping the `--allow-*`
flags. Node 24 then throws `ERR_MISSING_OPTION: --permission is
required` because the allow-list flags are orphaned. Dropping the
quotes keeps `--permission` a clean, standalone token.
2. NODE_OPTIONS override (#1036). The value only folded in a
`--node-options=` npm arg, never the caller's existing
`process.env.NODE_OPTIONS`, so npm replaced the user's global
NODE_OPTIONS instead of extending it. We now merge, in order,
process.env.NODE_OPTIONS, any `--node-options=` npm arg, and our
permission flags.
Extracted the construction into a pure, exported `buildNpmNodeOptionsArg`
helper and unit-tested both fixes.
John-David Dalton (jdalton)
force-pushed
the
jdalton/fix-npm-node-options-1160-1036
branch
from
July 30, 2026 15:14
3f272fb to
7fdbb3c
Compare
John-David Dalton (jdalton)
enabled auto-merge (squash)
July 30, 2026 16:40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two things go wrong today when you run
socket npm run buildon Node 24 or newer.It crashes. Node throws
TypeError [ERR_MISSING_OPTION]: --permission is requiredand the build dies. Nothing you did caused it —socket npmmangles its own flags on the way in.It silently discards your
NODE_OPTIONS. If you hadNODE_OPTIONS=--max-old-space-size=4096set globally, running your build throughsocket npmthrows it away. Builds that need the extra heap just fail, with no message saying why.Both come from one line in
src/shadow/npm-base.mts, and this PR fixes both. Fixes #1160 and #1036.Bug 1 — the quotes were never needed, and Next.js chokes on them (#1160)
The old line wrapped the value in literal single quotes:
`--node-options='${nodeOptionsArg ? nodeOptionsArg.slice(15) : ''}${cmdFlagsToString(permArgs)}'`shadowNpmBasespawns npm without a shell, so nothing ever interprets those quotes. They become part of the value npm assigns toNODE_OPTIONSfor lifecycle scripts:Node itself tolerates the garbled tokens. Consumers that re-tokenize
NODE_OPTIONSon whitespace and only honour"do not — Next.js's TypeScript build worker is the one users hit. It drops'--permission(it has a leading quote glued to it) while keeping the valid--allow-*flags. Node 24 then throws, because the allow-list flags are orphaned:Removing the quotes keeps
--permissiona clean, standalone token.Bug 2 — npm's
--node-optionsreplaces the inherited value, so ours clobbered the user's (#1036)The old value merged in a
--node-options=npm argument if one was present, but never the caller's existingprocess.env.NODE_OPTIONS. Since npm's--node-optionsconfig replaces the inheritedNODE_OPTIONSfor scripts, a globally-configuredNODE_OPTIONSwas silently dropped:The value now merges three sources, in this order:
process.env.NODE_OPTIONS— whatever the caller already had--node-options=npm argument, with its prefix strippedThe change — one pure exported helper, so both behaviours are testable without spawning
The construction moved out of the template literal and into a pure, exported
buildNpmNodeOptionsArg(envNodeOptions, nodeOptionsArg, permArgs), and the call site now uses it. Empty and falsy inputs are filtered out so the merge never leaves a stray separator.Concretely, for
NODE_OPTIONS=--max-old-space-size=4096 socket npm run buildon Node 24:--node-options='--permission --allow-child-process ...'→--permissiondropped downstream → crash, and--max-old-space-sizelost.--node-options=--max-old-space-size=4096 --permission --allow-child-process --allow-fs-read=* ...→ all tokens clean, user value preserved.What I checked — 7 new unit tests, plus typecheck and lint on the changed files
New
src/shadow/npm-base.test.mtscovers:--permissionstays a standalone token (socket npm run build crashes with ERR_MISSING_OPTION: --permission on Node v24 + Next.js #1160)process.env.NODE_OPTIONSis merged ahead of the permission flags ('socket npm' overrides NODE_OPTIONS #1036)--node-options=npm arg has its prefix stripped and is preservedRan:
pnpm exec vitest run src/shadow/npm-base.test.mts— 7/7 passpnpm run check:tsc— cleanoxlinton changed files — 0 errors (only pre-existingno-named-as-default-memberwarnings on unchanged lines)Branch scope — this targets
v1.x;maindoes not have the bugTargets
v1.x, the branch where the shadow-npm wrapper lives. The issues reproduce on v1.1.78.On
main,socket npmhands off to Socket Firewall and no longer builds this argument, somainis unaffected and needs no companion PR.Note
Medium Risk
Changes NODE_OPTIONS passed into npm lifecycle scripts (build/run), which can affect all downstream script processes, but the logic is isolated, well-tested, and targets known production bugs.
Overview
Fixes how
socket npmbuilds the--node-optionsflag for npm lifecycle scripts by replacing inline string templating withbuildNpmNodeOptionsArg.The value is no longer wrapped in single quotes, so
--permissionstays a real token when tools re-splitNODE_OPTIONSon whitespace (fixes Node ≥ 24ERR_MISSING_OPTIONon builds like Next.js, #1160). The helper also mergesprocess.env.NODE_OPTIONS, any existing npm--node-options=…arg, and Socket’s permission flags in that order, because npm’s flag replaces inheritedNODE_OPTIONSfor scripts (#1036).Adds
npm-base.test.mtswith seven unit tests for quoting, merge order, prefix stripping, and empty inputs.Reviewed by Cursor Bugbot for commit 3f272fb. Configure here.